package com.ruoyi.testmanagement.service.impl;

import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.testmanagement.domain.Cab;
import com.ruoyi.testmanagement.mapper.CabMapper;
import com.ruoyi.testmanagement.service.ICabService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class CabServiceImpl implements ICabService {
    private static final Logger log = LoggerFactory.getLogger(CabServiceImpl.class);
    @Autowired
    private CabMapper cabMapper;

    @Override
    public Cab selectCabById(Long id) {
        return cabMapper.selectCabById(id);
    }

    @Override
    public Cab selectCabByName(String name) {
        return cabMapper.selectCabByName(name);
    }

    @Override
    public List<Cab> selectCabList(Cab cab) {
        return cabMapper.selectCabList(cab);
    }

    @Override
    public int insertCab(Cab cab) {
        return cabMapper.insertCab(cab);
    }

    @Override
    public int updateCab(Cab cab) {
        return cabMapper.updateCab(cab);
    }

    @Override
    public int deleteCabById(Long cabId) {
        return cabMapper.deleteCabById(cabId);
    }

    @Override
    public int deleteCabByIds(Long[] Ids) {
        return cabMapper.deleteCabByIds(Ids);
    }

    @Override
    public String importCab(List<Cab> cabList, boolean updateSupport) {
        if (StringUtils.isNull(cabList) || cabList.size() == 0) {
            throw new ServiceException("导入cab数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();

        for (Cab cab : cabList) {
            try {
                // 验证是否存在这个CAB
                Cab c = cabMapper.selectCabByName(cab.getCabName());
                if (StringUtils.isNull(c)) {
                    this.insertCab(cab);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、cab " + cab.getCabName() + " 导入成功");
                } else if (updateSupport) {

                    this.updateCab(cab);
                    successNum++;
                    successMsg.append("<br/>" + successNum + "、cab " + cab.getCabName() + " 更新成功");
                } else {
                    failureNum++;
                    failureMsg.append("<br/>" + failureNum + "、cab " + cab.getCabName() + " 已存在");
                }
            } catch (Exception e) {
                failureNum++;
                String msg = "<br/>" + failureNum + "、cab " + cab.getCabName() + " 导入失败：";
                failureMsg.append(msg + e.getMessage());
                log.error(msg, e);
            }

        }
        if (failureNum > 0) {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        } else {
            successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
        }
        return successMsg.toString();
    }

    @Override
    public int updateCabStatus(Cab cab) {
        return cabMapper.updateCab(cab);
    }
}
